package com.product.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.product.system.domain.SysDept;
import com.product.system.mapper.SysDeptMapper;
import com.product.system.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    SysDeptMapper sysDeptMapper;

    @Override
    public List<Tree<String>>  deptList() {
        List<SysDept> list = sysDeptMapper.list();
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        for (SysDept sysDept : list) {
            Map<String,Object> map=new HashMap<>();
            TreeNode<String> treeNode = new TreeNode<>(sysDept.getDeptId() + "", sysDept.getParentId() + "", sysDept.getDeptName(), sysDept.getOrderNum());
            map.put("orderNum",sysDept.getOrderNum());
            map.put("status",sysDept.getStatus());
            String format = DateUtil.format(sysDept.getCreateTime(), "yyyy-MM-dd HH:mm:ss");
            map.put("createTime",format);
            treeNode.setExtra(map);
            nodeList.add(treeNode);
        }

        List<Tree<String>> deptList = TreeUtil.build(nodeList, "0");

        return deptList;
    }

    @Override
    public List<SysDept> getBy(SysDept sysDept) {
        List<SysDept> sysDepts = sysDeptMapper.getBy(sysDept);
        return sysDepts;
    }

    @Override
    public void saveDept(SysDept sysDept) {

        sysDept.setCreateTime(new Date());

        sysDeptMapper.saveDept(sysDept);
    }

    @Override
    public List<Tree<String>> getParent() {
        List<SysDept> list = sysDeptMapper.list();
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        for (SysDept sysDept : list) {
            Map<String,Object> map=new HashMap<>();
            TreeNode<String> treeNode = new TreeNode<>(sysDept.getDeptId() + "", sysDept.getParentId() + "", sysDept.getDeptName(), sysDept.getOrderNum());
            treeNode.setExtra(map);
            nodeList.add(treeNode);
        }
        List<Tree<String>> deptList = TreeUtil.build(nodeList, "0");
        return deptList;
    }

    @Override
    public void delDept(Integer id) {
        sysDeptMapper.delDept(id);
    }

    @Override
    public List<SysDept> getDeptList() {

        List<SysDept> list = sysDeptMapper.list();

        return list;
    }

    @Override
    public SysDept getParentById(Integer id) {
        SysDept sysDept=sysDeptMapper.getById(id);
        SysDept parent = sysDeptMapper.getById(sysDept.getParentId().intValue());
        return parent;
    }

    @Override
    public SysDept getById(Integer id) {
        SysDept sysDept = sysDeptMapper.getById(id);
        String phone = sysDept.getPhone();
        StringBuffer sb=new StringBuffer(phone);
        sb.replace(3, 7, "****");
        sysDept.setPhone(sb.toString());
        return sysDept;
    }

    @Override
    public void updateById(SysDept sysDept) {
        sysDeptMapper.updateDept(sysDept);
    }
}
